define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'census/reconciliation/index' + location.search,
                    add_url: 'census/reconciliation/add',
                    edit_url: 'census/reconciliation/edit',
                    del_url: 'census/reconciliation/del',
                    multi_url: 'census/reconciliation/multi',
                    import_url: 'census/reconciliation/import',
                    table: 'wanlshop_shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('店铺名称'),visible: false,addClass:'selectpage',extend:'data-source="census/shop" data-primary-key="id" data-show-field="shopname"'},
                        {field: 'order.adatetime', title: __('对账日期'), operate:'RANGE',datetimeFormat: "YYYY-MM-DD", addclass:'datetimerange',defaultValue: this.today(), formatter: Table.api.formatter.datetime},
                        {field: 'shopname', title: __('店铺名称'),operate:false},
                        {field: 'type', title: __('客户分类'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('门店状态'), searchList: {"normal":__('营业中'),"hidden":__('下线')}, formatter: Table.api.formatter.status},
                        {field: 'city', title: __('所属地区'), operate: false},
                        {field: 'count_goods_order', title: __('商品营业额'),operate:false},
                        {field: 'count_services_order', title: __('服务营业额'),operate:false},
                        {field: 'count_card_order', title: __('卡项营业额'),operate:false},
                        {field: 'count_refund_order', title: __('退款金额'),operate:false},
                        {field: 'count_total_order', title: __('总营业额'),operate:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $("button[type='submit']").css("background-color","red");
            $("button[type='submit']").css("border-color","red");

            //禁止点击抖索
            setInterval(function () {
                $('.searchit').attr('title','');
                $('.searchit').attr('data-toggle','').click(function () {
                    return false;
                });
            },100);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //定义方法返回今日开始结束日期
        today: function () {
            const dd = new Date();
            const y = dd.getFullYear();// 获取今日年份
            const m = String(dd.getMonth() + 1).padStart(2, '0'); // 获取今日月份
            const d = String(dd.getDate()).padStart(2, '0'); // 获取今日日期

            const startOfDay = `${y}-${m}-${d} 00:00:00`;
            const endOfDay = `${y}-${m}-${d} 23:59:59`;

            return startOfDay + ' - ' + endOfDay;
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'census/reconciliation/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'census/reconciliation/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'census/reconciliation/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
